package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author Austin Hsu
 * @Date 2022/9/20 22:54
 */
@RestController
public class CartController {

    @Autowired
    ICartService iCartService;

    @GetMapping("/shopping/carts")
    public ResponseData getCartListById(HttpServletRequest request) {      //是接收一个request
        String userInfo = request.getHeader("user_info");          // 得到一组Json字符串    {"uid":1, "username":"zs"}
        JSONObject object = JSON.parseObject(userInfo);       // 这个类继承了Map<String,Object>  所以结果是{"uid"=1,"username"="zs"}
        Long uid = Long.parseLong(object.get("uid").toString());
        List<CartProductDto> list = iCartService.getCartListById(uid);

        return new ResponseUtil().setData(list);
    }


    /**
     * 添加商品到购物车
     */
    @PostMapping("/shopping/carts")
    public ResponseData addToCart(@RequestBody CartRequest cartRequest) {      //是接收一个request
        iCartService.addToCart(cartRequest);
        return new ResponseUtil().setData1();      //stamp需要吗？？？？
    }

    @PutMapping("/shopping/carts")
    public ResponseData updateCart(@RequestBody CartRequest cartRequest) {      //是接收一个request
        iCartService.updateCartNum(cartRequest);

        return new ResponseUtil().setData1();
    }

    @DeleteMapping("shopping/carts/**/**")
    public ResponseData deleteCartItem(HttpServletRequest request) {      //是接收一个request
        String url = request.getRequestURL().toString();   //eg：http://124.71.209.81/shopping/carts/682/100047001
        //shopping/carts/{uid}/{pid}
        String str1 = url.substring(0, url.indexOf("carts/"));
        String str2 = url.substring(str1.length() + 6);                  //得到carts/之后的字符串 即得到682/100047001 即{uid}/{pid}
        String uid = str2.substring(0, str2.indexOf("/"));       //从682/100047001中得到682
        String pid = str2.substring(uid.length() + 1);            //得到10004701

        iCartService.deleteCartItem(Long.parseLong(uid), Long.parseLong(pid));

        return new ResponseUtil().setData1();
    }

    @DeleteMapping("/shopping/items/**")
    public ResponseData deleteCheckedItem(HttpServletRequest request) {      // http://124.71.209.81/shopping/items/682
        String url = request.getRequestURL().toString();
        String str1 = url.substring(0, url.indexOf("items/"));
        String str2 = url.substring(str1.length() + 6);         //682 即用户id

        iCartService.deleteCheckedItem(Long.parseLong(str2));
        return new ResponseUtil().setData1();
    }

    @PutMapping("/shopping/items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest request) {
        iCartService.checkAllCartItem(request);
        return new ResponseUtil().setData1();
    }
}
